import ExchangeVolume from "../base/ExchangeVolume";
import BaseModel from "../../framework/base/model/BaseModel";
import SDKManager from "../SDKManager";
import ToastController from "../../game/public/toast/ToastController";
import GameBoxItemModel from "../../game/logic/gamebox/GameBoxItemModel";
require("../../lib/AdvOTImage.js");//下载的附件放到相应的位置
// require('../../lib/wlad_sdk_game.min.js')
export default class LayaExchangeVolume extends BaseModel implements ExchangeVolume {


    private showFlag:boolean = false;

    isShowIcon(){
        return this.showFlag;
    }

    constructor(){
        super();
        this.init();
    }

    //初始化流量共享组件显示
    init(): void {
        AdvOTImage.start((isShowIcon) => {
            this.showFlag = isShowIcon;
            ToastController.instance().show(' isshowIcom '+isShowIcon)
            this.publish(SDKManager.START_GAME_ICON,isShowIcon)
        });
        // this.getAds(10,(result)=>{

        // })
    }

    /* 流量共享组件注册icon监听方法,
    ** gameIcon 为一个远端的图片路径，需要把路径赋值给挂接的显示对象即可
    */
    change(): void {
        AdvOTImage.change((isShowIcon, gameIcon) => {
            this.publish(SDKManager.UPDATE_GAME_ICON,isShowIcon,gameIcon)
            //这里控制组件挂接的精灵对象的显示控制，isShowIcon true 显示；false 隐藏
        });
    }

    jump(param?: any): void {
        AdvOTImage.navigateToMiniProgram();
    }


    getAds(num: number, func: (result: any) => void) {
        let list: GameBoxItemModel[] = []
        if (wx.wladGetAds) {
            wx.wladGetAds(num,  (res)=>{ //第⼀个参数为获取⼴告条数，第⼆个参数为获取成功后回调⽅法
                console.log(res);
                if (res.code == 0) {
                    if (res.data) {
                        for (let index = 0; index < res.data.length; index++) {
                            const element = res.data[index];
                            let item = new GameBoxItemModel()
                            item.setAppID(element.appid)
                            item.setIcon(element.logo)
                            item.setImage(element.img)
                            item.setName(element.name)
                            item.setDesc(element.desc)
                            list.push(item)
                        }
                    }

                    func(list)
                } else {
                    func(list)
                }

                
            })
        } else {
            func(list)
            console.log('wx.wladGetAds is null  ')
        }

    }
}
